<?php
/**
 *  ### Using the alias `i0' for the tree index table
 *
 *
 */
namespace Dclub\modules\Catalog;

class Action_SelectTreeBranch
    extends \Yz\modules\Generic\Action_Select
{

    const TABLE_INDEX = 'i0';
    const TABLE_INDEX_DEPTH = 'd0';
    const TABLE_TREE  = 't0';

    /**
     *  @var int ?
     */
    protected $originNodeId;


    /************************************
     *         Public methods          *
     ************************************/


    /**
     *  @param int $originNodeId
     *  @return this
     */
    public function setOriginNodeId( $originNodeId )
    {
        debug();
        $this->originNodeId = \Yz_Int::testNotNegative($originNodeId);
        return $this;
    }



    /**
     *  @return Yz_Hash
     */
    public function fetch()
    {
        return $this->getStatement()->fetchAll();
    }


    /************************************
     *       Protected methods          *
     ************************************/


    /**
     *  Modified SELECT statement creator
     *
     */
    protected function buildSelect()
    {
        debug();
        $sql = parent::buildSelect();
        $ti = self::TABLE_INDEX;
        $td = self::TABLE_INDEX_DEPTH;

        $from = $this->originNodeId;
        if (null === $from) {
            throw new \Yz_Exception("ID of branch origin node not set");
        }
        $sql
            ->where("{$ti}.parentId={$from}")
            ->where("{$td}.parentId={$td}.id") // depth select condition
            ->orderBy("{$td}.depth") // TODO do we need ordering at all?
            ;
        return $sql;
    }



    /** assign source tables and enforce correct join for the tables
     *  @param Yz_Hash $tableList
     *  @return chainable
     *  @throw Yz_Exception if table list empty
     *  @throw Yz_Exception if incorrect index table specification
     */
    protected function assignTables(\Yz_Hash $tableList)
    {
        parent::assignTables($tableList);

        $index = $tableList[self::TABLE_INDEX];

        if (is_string($index)) {
        } else if ($index instanceof \Yz_Hash) {
            $index = $index->name;
            if (null === $index) {
                throw new \Yz_Exception("No table name present in index table specification");
            }
        } else {
            if (null === $index) {
                throw new \Yz_Exception("No index table present in tablelist");
            }
            throw new \Yz_Exception("Unrecognized index table specification");
        }
        $join = array(
            'name' => $index,
            'join' => 'left',

            // ### USING ###
            'joinUsing' => 'id',
            );

        // the same table used to select depth
        $joinb = array(
            'name' => $index,
            'join' => 'left',

            // ### USING ###
            'joinUsing' => 'id',
            );
        $tableList[self::TABLE_INDEX] = new \Yz_Hash($join);
        $tableList[self::TABLE_INDEX_DEPTH] = new \Yz_Hash($joinb);
        return $this;
    }


}
